package com.hbt.android.kotlin.ui.home

import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.GridView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.hbt.android.kotlin.R
import com.hbt.android.kotlin.databinding.FragmentHomeBinding
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.learn_item_view_linear_vertical.view.*

class HomeFragment : Fragment(R.layout.fragment_home) {


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recycler_view.layoutManager =
                // 水平排列
//            LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                // 垂直排列
//            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)


                // 网格布局
//            GridLayoutManager(context, 2)
            // 瀑布布局
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)

        recycler_view.adapter = MyAdapter()
    }

    inner class MyAdapter : RecyclerView.Adapter<MyViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
//            val itemView = LayoutInflater.from(context).inflate(R.layout.learn_item_view_linear_horizontal, parent, false)
//            val itemView = LayoutInflater.from(context).inflate(R.layout.learn_item_view_linear_vertical, parent, false)
            val itemView = LayoutInflater.from(context).inflate(R.layout.learn_item_view_grid, parent, false)

            return MyViewHolder(itemView)
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            holder.itemView.item_image.setImageResource(R.drawable.ic_launcher_foreground)

            // context!!  代表context不会为空
//            holder.itemView.item_image.setImageDrawable(ContextCompat.getDrawable(context!!, R.drawable.android))
//
//            holder.itemView.item_image.setImageBitmap(BitmapFactory.decodeResource(context!!.resources, R.drawable.android))

            if (position and 1 === 1) {
                holder.itemView.item_message.setSingleLine(false)
            } else {
                holder.itemView.item_message.setSingleLine(true)
            }

            holder.itemView.item_title.text = "[${position}]移动端架构师"
            holder.itemView.item_message.text = "移动端架构师，阿斯弗是否偶的完全九二， 阿斯顿佛我去。"
        }

        // 设置元素个数
        override fun getItemCount(): Int {
            return 20
        }

    }

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {

    }
}


//class HomeFragment : Fragment() {

//  private lateinit var homeViewModel: HomeViewModel
//private var _binding: FragmentHomeBinding? = null
//  // This property is only valid between onCreateView and
//  // onDestroyView.
//  private val binding get() = _binding!!
//
//  override fun onCreateView(
//    inflater: LayoutInflater,
//    container: ViewGroup?,
//    savedInstanceState: Bundle?
//  ): View? {
//    homeViewModel =
//            ViewModelProvider(this).get(HomeViewModel::class.java)
//
//    _binding = FragmentHomeBinding.inflate(inflater, container, false)
//    val root: View = binding.root
//
//    val textView: TextView = binding.textHome
//    homeViewModel.text.observe(viewLifecycleOwner, Observer {
//      textView.text = it
//    })
//    return root
//  }
//
//override fun onDestroyView() {
//        super.onDestroyView()
//        _binding = null
//    }
//}